package com.ts.api.http.org.base;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.digest.MD5;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.http.org.base.entity.apply.BaseOrgApplyXingLu;
import com.ts.api.http.org.base.entity.apply.BaseOrgApplyXingLuData;
import com.ts.api.http.org.base.entity.dataexchange.BaseOrgDataExchangeXingLU;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMaskConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskApply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskDataExchange;

/**
 *  鑫鹿
 *
 *  @author WJB
 */
public class ApiOrgPhoneMaskXinLu implements ApiOrgPhoneMaskConvertHandler {

    private final String tenantId;
    private final String platformId;
    /** 密钥 */
    private final String key;

    public ApiOrgPhoneMaskXinLu(String tenantId, String platformId, String key) {
        this.tenantId = tenantId;
        this.platformId = platformId;
        this.key = key;
    }

    @Override
    public String dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) {
        BaseOrgDataExchangeXingLU baseOrgDataExchangeXingLU = new BaseOrgDataExchangeXingLU();
        baseOrgDataExchangeXingLU.setTenantId(tenantId);
        baseOrgDataExchangeXingLU.setType(1);
        baseOrgDataExchangeXingLU.setPhone(apiDataExchangeParameter.getPhoneMask());

        return JsonUtils.toJson(baseOrgDataExchangeXingLU);
    }

    @Override
    public RespOrgPhoneMaskDataExchange respDataExchange(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }
        return RespOrgPhoneMaskDataExchange.ofSuc();
    }


    @Override
    public String apply(ApiApplyParameter apiApplyParameter) {
        BaseOrgApplyXingLu baseOrgApplyXingLu = new BaseOrgApplyXingLu();
        baseOrgApplyXingLu.setPlatformId(platformId);
        baseOrgApplyXingLu.setTenantId(tenantId);

        BaseOrgApplyXingLuData apiOrgXingLu = new BaseOrgApplyXingLuData();
        apiOrgXingLu.setPhone(apiApplyParameter.getPhone());
        apiOrgXingLu.setName(apiApplyParameter.getUserName());

        //  身份证
        if (ValidParam.isNotEmpty(apiApplyParameter.getIdcard())) {
            apiOrgXingLu.setIdcard(apiApplyParameter.getIdcard());
        }

        //  贷款额度
        if (ValidParam.isNotEmpty(apiApplyParameter.getLoanAmount())) {
            String money = ChooseEq.create()
                .when(apiApplyParameter.getLoanAmount(), "1", "30000")
                .when(apiApplyParameter.getLoanAmount(), "2", "50000")
                .when(apiApplyParameter.getLoanAmount(), "3", "100000")
                .when(apiApplyParameter.getLoanAmount(), "4", "200000")
            .end();
            apiOrgXingLu.setCustomerAmount(money);
        }

        //  客户类型
        String career = apiApplyParameter.getCareer();
        if (ValidParam.isEmpty(career)) {
            //  无数据为个人用户
            apiOrgXingLu.setCustomerType("个人");
        } else {
            String choose = ChooseEq.create()
                    //  如果是企业就为企业用户
                    .when(career, "3", "企业")
                    //  个人用户
                    .end("个人");
            apiOrgXingLu.setCustomerType(choose);
        }

        //  房产情况
        if (ValidParam.isNotEmpty(apiApplyParameter.getRealEstateInfo())) {
            String house = ChooseEq.create()
                    .when(apiApplyParameter.getRealEstateInfo(), "1", "无")
                    .end("有");
            apiOrgXingLu.setHouseNum(house);
        }

        //  车产
        if (ValidParam.isNotEmpty(apiApplyParameter.getCarInfo())) {
            String car = ChooseEq.create()
                    .when(apiApplyParameter.getCarInfo(), "1", "无")
                    .end("有");
            apiOrgXingLu.setVehicleNum(car);
        }

        //  保单
        if (ValidParam.isNotEmpty(apiApplyParameter.getInsurancePolicy())) {
            String baodanIs = ChooseEq.create()
                    .when(apiApplyParameter.getInsurancePolicy(), "1", "无")
                    .end("有");
            apiOrgXingLu.setPolicyNum(baodanIs);
        }

        //  社保
        if (ValidParam.isNotEmpty(apiApplyParameter.getSocialSecurity())) {
            String shebao = ChooseEq.create()
                    .when(apiApplyParameter.getSocialSecurity(), "1", "无")
                    .end("有");
            apiOrgXingLu.setSocialSecurityNum(shebao);
        }

        //  公积金
        if (ValidParam.isNotEmpty(apiApplyParameter.getProvidentFund())) {
            String gongjijin = ChooseEq.create()
                    .when(apiApplyParameter.getProvidentFund(), "1", "无")
                    .end("有");
            apiOrgXingLu.setAccumulationFundNum(gongjijin);
        }

        //  银行代发
        if (ValidParam.isNotEmpty(apiApplyParameter.getSalaryPayMethod())) {
            String isbankpay = ChooseEq.create()
                    .when(apiApplyParameter.getSalaryPayMethod(), "1", "是")
                    .end("否");
            apiOrgXingLu.setIncomeNum(isbankpay);
        }

        String areaCode = apiApplyParameter.getAreaCode();
        //  省编码
        apiOrgXingLu.setProvinceCode(areaCode.substring(0, 2) + "0000");
        //  市编码
        apiOrgXingLu.setCityCode(areaCode);

        //  额外信息
        StringBuilder extendOne = new StringBuilder();

        //  信用记录-逾期记录
        if (ValidParam.isNotEmpty(apiApplyParameter.getOverdue())) {
            String text = ChooseEq.create()
                .when(apiApplyParameter.getOverdue(), "1", "无逾期")
                .when(apiApplyParameter.getOverdue(), "2", "有逾期")
                .when(apiApplyParameter.getOverdue(), "3", "一年以内无逾期")
            .end();
            extendOne.append("信用记录：").append(text).append("\n");
        }

        //  芝麻分
        if (ValidParam.isNotEmpty(apiApplyParameter.getSesameCredit())) {
            String text = ChooseEq.create()
                .when(apiApplyParameter.getSesameCredit(), "1", "600分以下")
                .when(apiApplyParameter.getSesameCredit(), "2", "600~649分")
                .when(apiApplyParameter.getSesameCredit(), "3", "650~699分")
                .when(apiApplyParameter.getSesameCredit(), "4", "700分以上")
            .end();
            extendOne.append("芝麻分：").append(text).append("\n");
        }
        //  职业
        if (ValidParam.isNotEmpty(apiApplyParameter.getCareer())) {
            String text = ChooseEq.create()
                .when(apiApplyParameter.getCareer(), "1", "上班族")
                .when(apiApplyParameter.getCareer(), "2", "自由职业")
                .when(apiApplyParameter.getCareer(), "3", "企业主（有营业执照）")
                .when(apiApplyParameter.getCareer(), "4", "公务员或事业单位")
            .end();
            extendOne.append("职业：").append(text).append("\n");
        }
        //  学历
        if (ValidParam.isNotEmpty(apiApplyParameter.getEducationAttainment())) {
            String text = ChooseEq.create()
                .when(apiApplyParameter.getEducationAttainment(), "1", "初中及以下")
                .when(apiApplyParameter.getEducationAttainment(), "2", "中专")
                .when(apiApplyParameter.getEducationAttainment(), "3", "高中")
                .when(apiApplyParameter.getEducationAttainment(), "4", "大专")
                .when(apiApplyParameter.getEducationAttainment(), "5", "本科")
                .when(apiApplyParameter.getEducationAttainment(), "6", "研究生及以上")
            .end();
            extendOne.append("学历：").append(text).append("\n");
        }
        //  收入形式
        if (ValidParam.isNotEmpty(apiApplyParameter.getSalaryPayMethod())) {
            String text = ChooseEq.create()
                .when(apiApplyParameter.getSalaryPayMethod(), "1", "银行代发打卡")
                .when(apiApplyParameter.getSalaryPayMethod(), "2", "现金收入")
                .when(apiApplyParameter.getSalaryPayMethod(), "3", "个人转账")
            .end();
            extendOne.append("收入形式：").append(text).append("\n");
        }
        //  性别
        String sex = ChooseEq.create()
                .when(apiApplyParameter.getSex(), "0", "男")
                .when(apiApplyParameter.getSex(), "1", "女")
                .end("无");
        extendOne.append("性别：").append(sex).append("\n");
        //  年龄
        if (ValidParam.isNotEmpty(apiApplyParameter.getAge())) {
            ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
            String text = apiFieldConverter.getAge();
            extendOne.append("年龄：").append(text).append("\n");
        }
        //  拓展信息
        apiOrgXingLu.setExtendOne(extendOne.toString());

        //  加密数据
        AES ecb = AES.createEcb(key.getBytes());
        String data = JsonUtils.toJson(apiOrgXingLu);
        String encryptData = ecb.encryptToBase64Str(data);
        baseOrgApplyXingLu.setData(encryptData);

        String str = "data=" + encryptData +
                "&platformId=" + platformId +
                "&tenantId=" + tenantId +
                "&key=" + key;
        String sign = MD5.create().digestToHex(str);
        baseOrgApplyXingLu.setSign(sign);

        return JsonUtils.toJson(baseOrgApplyXingLu);
    }

    @Override
    public RespOrgPhoneMaskApply respApply(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMaskApply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMaskApply.ofFail();
        }
        return RespOrgPhoneMaskApply.ofSuc();
    }
}
